<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="ID">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="柜机编码" prop="machineNumber">
        <el-input
          v-model="queryParams.machineNumber"
          placeholder="请输入编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备型号" prop="isModel">
        <el-input
          v-model="queryParams.isModel"
          placeholder="请输入设备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版本号" prop="systemVersion">
        <el-input
          v-model="queryParams.systemVersion"
          placeholder="请输入版本号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在地区" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="所在地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="柜机状态" prop="machineStatus">
        <el-input
          v-model="queryParams.machineStatus"
          placeholder="柜机状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存储类型" prop="storageStatus">
        <el-input
          v-model="queryParams.storageStatus"
          placeholder="存储类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="箱格状态" prop="boxStatus">
        <el-input
          v-model="queryParams.boxStatus"
          placeholder="箱格状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button style="margin-left: 10px" @click="routerAddMore()" type="primary">批量添加</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['machine:machine:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['machine:machine:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['machine:machine:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['machine:machine:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="machineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="ID" prop="id" width="120">
        <template slot-scope="scope">
          <!-- 使用padStart方法来确保ID是9位的，如果不足则用0填充 -->
          <el-link :underline="false" @click="handleClick(scope.row)" style="color:blue">{{ scope.row.id.toString().padStart(9, '0') }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="柜机编码" align="center" prop="machineNumber" />
      <el-table-column label="所在地区" align="center" prop="position" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="设备型号" align="center" prop="isModel" />
      <el-table-column label="箱格" prop="" width="120">
        <template slot-scope="scope">
<!--          <el-link :underline="false" @click="goToBoxDetail(scope.row.id)" style="color:blue">{{ scope.row.useCount==null ?  '0' : scope.row.useCount }}/20</el-link>-->
<!--          <el-link :underline="false" @click="goToBoxDetail(scope.row.id)" style="color:blue">{{ scope.row.useCount == null ? '0' : scope.row.useCount}}/20</el-link>-->
          <el-link :underline="false" @click="goToBoxDetail(scope.row.id)" style="color:blue">{{ scope.row.useCount == null ? '0' : scope.row.useCount}}/20</el-link>
        </template>
      </el-table-column>
<!--      <el-table-column label="存储类型" prop="storageStatus" width="120">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tag v-if="scope.row.storageStatus == 0" type="success">常温</el-tag>-->
<!--          <el-tag v-else-if="scope.row.storageStatus == 1" type="danger">冷藏</el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="系统版本" align="center" prop="systemVersion" />-->
      <el-table-column label="安装日期" align="center" prop="installDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.installDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开通时间" align="center" prop="openDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.openDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="箱门状态" prop="boxStatus" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.boxStatus === 0" type="success">正常</el-tag>
          <el-tag v-else-if="scope.row.boxStatus === 1" type="danger">异常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="柜机状态" prop="machineStatus" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.machineStatus === 0" type="success">正常</el-tag>
          <el-tag v-else-if="scope.row.machineStatus === 1" type="danger">已禁用</el-tag>
          <el-tag v-else-if="scope.row.machineStatus === 2" type="warning">已关机</el-tag>
          <el-tag v-else-if="scope.row.machineStatus === 3" type="warning">重启中</el-tag>
          <el-tag v-else-if="scope.row.machineStatus === 4" type="warning">未开通</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="是否删除" align="center" prop="isDel" />-->

<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['machine:machine:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['machine:machine:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" width="80">
        <template slot-scope="scope">
          <el-button
            @click="setId(scope.row.id)"
            type="text"
            size="small">
            <span style="color: #409EFF;font-size: larger; font-weight: bold;">&hellip;</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div v-if="showDiv" class="fixed-div">
      <el-button @click="status=0;updateStatuss()" icon="el-icon-caret-right" style="color: deepskyblue;margin:0 auto;border-style: none;margin-top: 30px"><font color="black">开通</font></el-button>
      <el-button @click="status=1;updateStatuss()" icon="el-icon-remove-outline" style="color: red;margin:0 auto;border-style: none"><font color="black">禁用</font></el-button>
      <el-button @click="status=0;updateStatuss()" icon="el-icon-caret-right" style="color: green;margin:0 auto;border-style: none"><font color="black">启用</font></el-button>
      <el-button @click="status=0;updateStatuss()" icon="el-icon-loading" style="color: blue;margin:0 auto;border-style: none"><font color="black">重启</font></el-button>
      <el-button @click="status=2;updateStatuss()" icon="el-icon-switch-button" style="color: red;margin:0 auto;border-style: none"><font color="black">关机</font></el-button>
      <el-button @click="status=0;updateStatuss()" icon="el-icon-switch-button" style="color: green;margin:0 auto;border-style: none"><font color="black">开机</font></el-button>
    </div>

    <!-- 添加或修改柜机管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="柜机编码" prop="machineNumber">
          <el-input v-model="form.machineNumber" placeholder="请输入柜机编码" />
        </el-form-item>
        <el-form-item label="所在地区" prop="position">
          <el-input v-model="form.position" placeholder="请输入所在地区" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="设备型号" prop="isModel">
          <el-input v-model="form.isModel" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="存藏类型" style="width: 300px" prop="storageStatus">
          <el-select v-model="form.storageStatus" placeholder="请选择">
            <el-option label="冷藏" value="1"></el-option>
            <el-option label="常温" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="安装日期" prop="installDate">
          <el-date-picker clearable
            v-model="form.installDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择安装日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否开通" prop="machineStatus">
          <el-radio-group v-model="form.machineStatus">
            <el-radio :label="0">已开通</el-radio>
            <el-radio :label="4">未开通</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="开通时间" prop="openDate" style="width: 300px">
          <el-date-picker :disabled="form.machineStatus===4"
                          v-model="form.openDate"
                          type="datetime"
                          placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="主柜尺寸">
          <el-input v-model="form.primaryWidth" autocomplete="off" style="width: 90px" placeholder="请填写宽"></el-input>
          <el-input v-model="form.primaryDepth" autocomplete="off" style="width: 90px" placeholder="请填写深"></el-input>
          <el-input v-model="form.primaryHeight" autocomplete="off" style="width: 90px" placeholder="请填写高"></el-input>
        </el-form-item>
        <el-form-item label="副柜尺寸">
          <el-input v-model="form.secondaryWidth" autocomplete="off" style="width: 90px" placeholder="请填写宽"></el-input>
          <el-input v-model="form.secondaryDepth" autocomplete="off" style="width: 90px" placeholder="请填写深"></el-input>
          <el-input v-model="form.secondaryHeight" autocomplete="off" style="width: 90px" placeholder="请填写高"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMachine, getMachine, delMachine, addMachine, updateMachine,updateStatus,findMachineById } from "@/api/machine/machine";
import {resetForm} from "@/utils/ruoyi";
// import {updateStatus} from "@/api/machine/machine";

export default {
  name: "Machine",
  data() {
    return {
      status: 0,
      statusId:'',
      showDiv: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 柜机管理表格数据
      machineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id:null,
        boxStatus: null,
        machineStatus: null,
        storageStatus: null,
        isModel:null,
        systemVersion:null,
        position:null,
        address:null,
        installDate:null,
        openDate:null,


      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    goToBoxDetail(id){
      this.$router.push({path: "/boxList", query: {id: id}});
    },
    handleClick(row){
      this.$router.push({path: "/machinedetail",query:{id:row.id}});
    },
    // 批量添加
    routerAddMore(){
      this.$router.push({path: "/AddMore"});
    },
    // 状态修改
    updateStatuss(){
      if(this.statusId===''||this.statusId==null){
        this.$message({
          message: '请选择设备',
          type: 'warning'
        });
      }else{
        this.showDiv = !this.showDiv;
        this.$confirm('是否确认该操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const data = {
            id: this.statusId,
            machineStatus: this.status
          }
          updateStatus(data).then(res => {
              this.$modal.msgSuccess("修改成功")
              this.getList();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      }
    },
    // resetForm,
    setId(id) {
      console.log(id);
      this.statusId=id;
      this.showDiv = !this.showDiv;
    },
    /** 查询柜机管理列表 */
    getList() {
      this.loading = true;
      listMachine(this.queryParams).then(response => {
        this.machineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        machineNumber: null,
        position: null,
        address: null,
        isModel: null,
        installDate: null,
        openDate: null,
        isType: null,
        boxStatus: null,
        machineStatus: null,
        isDel: null,
        // systemVersion: null,
        storageStatus:null,
        primaryWidth:null,
        primaryDepth:null,
        primaryHeight:null,
        secondaryWidth:null,
        secondaryDepth:null,
        secondaryHeight:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加柜机管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMachine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改柜机管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMachine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMachine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除柜机管理编号为"' + ids + '"的数据项？').then(function() {
        return delMachine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('machine/machine/export', {
        ...this.queryParams
      }, `machine_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
